
package com.taku.adprofadapter;


import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.adprof.sdk.api.AdError;
import com.adprof.sdk.api.AdRequest;
import com.adprof.sdk.api.BannerAd;
import com.adprof.sdk.api.BannerAdListener;
import com.anythink.banner.unitgroup.api.CustomBannerAdapter;
import com.anythink.core.api.ATAdConst;
import com.anythink.core.api.ATBiddingListener;
import com.anythink.core.api.ATBiddingResult;
import com.anythink.core.api.ATInitMediation;
import com.anythink.core.api.MediationInitCallback;

import java.util.Map;


public class AdprofBannerAdapter extends CustomBannerAdapter implements BannerAdListener {
    public static String TAG = AdprofInitNetwork.TAG;

    BannerAd mBannerAd;

    String app_id;
    String slot_Id;

    @Override
    public boolean startBiddingRequest(Context context, Map<String, Object> serverExtra, Map<String, Object> localExtra, ATBiddingListener biddingListener) {
        Log.d(TAG, "\n banner startBiddingRequest   serverExtra = " + serverExtra + "   localExtra = " + localExtra + "   biddingListener = " + biddingListener);

        loadCustomNetworkAd(context, serverExtra, localExtra);

        return true;
    }

    @Override
    public void loadCustomNetworkAd(Context context, Map<String, Object> serverExtra, Map<String, Object> localExtra) {


        app_id = ATInitMediation.getStringFromMap(serverExtra, "app_id");
        slot_Id = ATInitMediation.getStringFromMap(serverExtra, "slot_id");

        if (TextUtils.isEmpty(slot_Id)) {
            slot_Id = ATInitMediation.getStringFromMap(serverExtra, "unit_id");
        }

        Log.d(TAG, "loadCustomNetworkAd: mAppId = " + app_id + "  mADUnitId = " + slot_Id);

        if (TextUtils.isEmpty(app_id)) {
            notifyATLoadFail("", "Adprof app_id is empty.");
            return;
        }

        AdprofInitNetwork.getInstance().initSDK(context, serverExtra, new MediationInitCallback() {
            @Override
            public void onSuccess() {
                loadAd();
            }

            @Override
            public void onFail(String errorMsg) {
                notifyATLoadFail("", errorMsg);
            }
        });
    }

    private void loadAd() {
        Log.d(TAG, "banner  loadAd start ");

        AdRequest adRequest = new AdRequest.Builder()
                .setCodeId(slot_Id)
                .build();

        mBannerAd = new BannerAd(adRequest, this, true, true);

        mBannerAd.loadAd();
    }

    @Override
    public boolean isAdReady() {
        if (mBannerAd != null) {
            return mBannerAd.isReady();
        }
        return false;
    }

    @Override
    public View getBannerView() {
        Log.d(TAG, "banner  getBannerView ");

        if (mBannerAd != null) {
            return mBannerAd.getBannerView();
        }
        return null;
    }

    @Override
    public String getNetworkPlacementId() {
        return slot_Id;
    }

    @Override
    public String getNetworkSDKVersion() {
        return AdprofInitNetwork.getInstance().getNetworkVersion();
    }

    @Override
    public String getNetworkName() {
        return AdprofInitNetwork.getInstance().getNetworkName();
    }

    @Override
    public void onBannerAdLoadSuccess() {
        Log.d(TAG, "banner  onBannerAdLoadSuccess ");

        try {
            if (mBiddingListener != null) {
                int ecpm = mBannerAd.getBidPrice();

                AdprofBiddingNotify biddingNotice = new AdprofBiddingNotify(mBannerAd);

                mBiddingListener.onC2SBiddingResultWithCache(ATBiddingResult.success(ecpm, System.currentTimeMillis() + "", biddingNotice, ATAdConst.CURRENCY.RMB_CENT), null);
            }
        } catch (Throwable e) {
            Log.d(TAG, "onBannerAdLoadSuccess: e = " + Log.getStackTraceString(e));
        }
    }

    @Override
    public void onBannerAdShow() {
        Log.d(TAG, "banner  onBannerAdShow ");

        if (mImpressionEventListener != null) {
            mImpressionEventListener.onBannerAdShow();
        }

    }

    @Override
    public void onBannerAdClick() {
        Log.d(TAG, "banner  onBannerAdClick ");

        if (mImpressionEventListener != null) {
            mImpressionEventListener.onBannerAdClicked();
        }
    }

    @Override
    public void onBannerAdClosed() {
        Log.d(TAG, "banner  onBannerAdClosed ");

        if (mImpressionEventListener != null) {
            mImpressionEventListener.onBannerAdClose();
        }

    }

    @Override
    public void onBannerAdLoadError(AdError adError) {
        Log.d(TAG, "banner  onBannerAdLoadError " + adError);

        if (mLoadListener != null && adError != null) {
            mLoadListener.onAdLoadError(adError.getErrorCode() + "", adError.getMessage());
        }

    }

    @Override
    public void onBannerAdShowError(AdError adError) {
        Log.d(TAG, "banner  onBannerAdShowError " + adError);
    }

    @Override
    public void destory() {
        try {
            if (mBannerAd != null) {
                mBannerAd.destroyAd();
                mBannerAd = null;
            }
        } catch (Exception e) {
            Log.d(TAG, Log.getStackTraceString(e));
        }
    }
}
